package cn.yanxiaohui.service.web.pattern.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * 动态代理的代理类
 * 	核心类
 * 		InvocationHandler--增强方法
 * 		Proxy--生成代理对象
 * @author yanxh
 *
 */
public class TGP implements InvocationHandler, Game{
	private Game game;

	public TGP(Game game) {
		this.game = game;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("TGP开始优化游戏环境...");
		Object invoke = method.invoke(game, args);
		System.out.println("游戏已正常启动...");
		return invoke;
	}

	@Override
	public void play() {
		Game g = (Game) Proxy.newProxyInstance(game.getClass().getClassLoader(), game.getClass().getInterfaces(), this);
		g.play();
	}
}
